package com.group.flagtivities;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class Password extends Activity implements OnClickListener {

	private static EditText password_old;
	private static EditText password_new;
	private static Button button;
	private static ProgressBar buttonIndicator;
	
	@Override
	public void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.password);
		
		findViews();
		
		TextUtil.setFont((ViewGroup)findViewById(R.id.password), getAssets());
		findViewById(R.id.password).setOnClickListener(this);
	}
	
	private void shake() {
		Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		findViewById(R.id.password_container).startAnimation(shake);
	}
	
	private void setPassword() {
		final String passwordOld = password_old.getText().toString().trim();
		final String passwordNew = password_new.getText().toString().trim();
		
		password_old.setText(passwordOld);
		password_new.setText(passwordNew);
		
		if (!passwordOld.isEmpty() && !passwordNew.isEmpty()) {
			findViewById(R.id.password).setClickable(false);
			password_old.setEnabled(false);
			password_new.setEnabled(false);
			button.setEnabled(false);
			buttonIndicator.setVisibility(View.VISIBLE);
			Timer t = new Timer();
			t.schedule(new TimerTask() {
				public void run() {
					WebUtil.setPassword(Flagtivities.user_id, passwordOld, passwordNew);
					finish();
				}
			}, 1000);
		}else {
			shake();
		}
	}
	
	private void findViews() {
		password_old = (EditText) findViewById(R.id.password_old);
		password_new = (EditText) findViewById(R.id.password_new);
		button = (Button) findViewById(R.id.button);
		buttonIndicator = (ProgressBar) findViewById(R.id.buttonIndicator);
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()) {
		case R.id.password:
			finish();
			break;
		case R.id.button:
			setPassword();
			break;
		}
		
	}
	
}
